Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always return the same value when resolving requests multiple times #522

Merged
merged 5 commits into from
Aug 15, 2024

Conversation

pablopalacios
Copy link
Contributor

@pablopalacios pablopalacios commented Aug 15, 2024

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

I found a bug in our system when resolving a request multiple times. The first time it was resolved it was fine (and so fetchr tests), but at the second time it would resolve with an undefined value. The code was looking something like this:

const request = fetchr.read('foo', { bar: 42 }); // request was triggered
track(request); // would call then internally
return request; // the consumer of this function would also call then

Fixing it was simple, we just had to keep a reference to the fetch promise in httpRequest and always return _request.then in FetchrHttpRequest.then method.

However, that broke the abort support since we were not returning an instance of FetchrHttpRequest anymore to the caller. To solve that, it was also necessary to store a reference to FetchrHttpRequest in the Request class in fetchr.client.js.

I added a bunch of more tests to cover this and other cases as well.

We shouldn't warn if the user does something like:

fetchr.read('foo', { id: 42 });

We were doing it since internally we were calling end with an
undefined callback.
- Fetchr({}) => Fetchr()
- .read('foo', null) => .read('foo')
Before this commit, when calling fetchr on the client using the
promise API and resolving it multiple times would lead to undefined
values.

We now make sure to always return the fetch promise in
httpRequest. But doing so, broke the abort behavior since we would not
return an FetchrHttpRequest to the user from fetchr methods. To fix
that, we now save an instance of FetchrHttpRequest in the Request
class and added an abort method to the later one to call
FetchrHttpRequest.abort method.
Copy link
Contributor

@redonkulus redonkulus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting bug find, changes look good to me.

@redonkulus redonkulus merged commit 9cecb4c into yahoo:main Aug 15, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants